package ay.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import ay.util.AnaService;

public class ShowAnaWindow {

	private JFrame frmMiui;
	private AnaService as;
	private Random r = new Random();
	private ShowText st = new ShowText();
	private ShowImage si = new ShowImage();
	ImageIcon img = new ImageIcon();
	private String defaultFontName = "汉仪乐喵体";
	private int fontSize= 20;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					ShowAnaWindow window = new ShowAnaWindow();
					window.frmMiui.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ShowAnaWindow() {
		as = AnaService.getInstance();
		initialize();
		taggleAna();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMiui = new JFrame();
		setFont();
		frmMiui.setTitle("精彩语录 -- by Anyang ");
		frmMiui.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				taggleAna();
			}
		});
		frmMiui.getContentPane().setForeground(new Color(255, 255, 255));
		frmMiui.setBounds(100, 100, 485, 713);
		frmMiui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBackground(new Color(255, 255, 255));
		GroupLayout groupLayout = new GroupLayout(frmMiui.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap().addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE).addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap().addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE).addContainerGap()));
		txtpnHello.setForeground(new Color(47, 79, 79));
		SimpleAttributeSet attr = new SimpleAttributeSet();
		StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
		txtpnHello.setParagraphAttributes(attr, false);
		txtpnHello.setFont(new Font(defaultFontName, Font.PLAIN, fontSize));
		txtpnHello.setBackground(new Color(255, 255, 255));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);

		lblNewLabel.setBackground(new Color(240, 255, 240));
		GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
		gl_desktopPane.setHorizontalGroup(
			gl_desktopPane.createParallelGroup(Alignment.LEADING)
				.addComponent(txtpnHello, GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
				.addGroup(gl_desktopPane.createSequentialGroup()
					.addGap(2)
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE))
		);
		gl_desktopPane.setVerticalGroup(
			gl_desktopPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_desktopPane.createSequentialGroup()
					.addComponent(txtpnHello, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 595, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(61, Short.MAX_VALUE))
		);
		desktopPane.setLayout(gl_desktopPane);
		frmMiui.getContentPane().setLayout(groupLayout);
		frmMiui.setLocationRelativeTo(null);
		frmMiui.setIconImage(frmMiui.getToolkit().getImage(ShowAnaWindow.class.getResource("/media/logo2.png")));
	}

	private void taggleAna() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Color c = new Color(r.nextInt(235), r.nextInt(235), r.nextInt(235));
				txtpnHello.setForeground(c);
				frmMiui.getContentPane().setBackground(c);
				if (st.isAlive()) {
					st.stop();
					// System.out.println("显示文本 停止了");
				}
				if (si.isAlive()) {
					si.stop();
					// System.out.println("显示图片 停止了");
				}
				Map<String, String> map = as.getAna();
				st = new ShowText(map.get("content"));
				st.start();
				si = new ShowImage(map.get("pic"));
				si.start();
				frmMiui.setIconImage(frmMiui.getToolkit().getImage(ShowAnaWindow.class.getResource("/media/logo.png")));
			}
		}).start();
	}

	class ShowText extends Thread {
		String content = "";

		ShowText(String content) {
			this.content = content;
		}

		ShowText() {
			super();
		}

		@Override
		public void run() {
			for (int i = 0; i < content.length(); i++) {
				txtpnHello.setText(content.substring(0, i + 1));
				try {
					Thread.sleep(128);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	class ShowImage extends Thread {
		String pic = "";

		ShowImage(String pic) {
			this.pic = pic;
		}

		ShowImage() {
			super();
		}

		@Override
		public void run() {
			try {
					lblNewLabel.setIcon(new ImageIcon(frmMiui.getToolkit().getImage(ShowAnaWindow.class.getResource("/media/loading.gif"))));
					img = new ImageIcon(new URL(pic));
					img.setImage(img.getImage().getScaledInstance(435, 585, Image.SCALE_DEFAULT));
					lblNewLabel.setIcon(img);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void setFont(){
		try{
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	        String[] fonts = ge.getAvailableFontFamilyNames();
	        if(Arrays.binarySearch(fonts, defaultFontName)<0){
	        	 if(Arrays.binarySearch(fonts, "汉仪乐喵体简")>0){
	 	        	defaultFontName = "汉仪乐喵体简";
	 	        }else{
	 	        	defaultFontName = "宋体";
	 	        	fontSize = 19;
	 	        }
	        	
	        }
		}catch(Exception e){
		}
	}
	JTextPane txtpnHello = new JTextPane();
	JLabel lblNewLabel = new JLabel();
}
